import { config as wx_config, error, ready } from 'tencent-wx-jssdk';
import service from '../../browser/msg/service';

const PAYSERVICE = 'payservice.newthirdpayment';

interface IWeixinConfigParam {
	appid: string;		// 公众号的唯一标识
	timestamp: number;	// 生成签名的时间戳
	nonce: string;		// 生成签名的随机串
	signature: string;	// 签名
}

export default function config(spaceid: string, jsApiList: jWeixin.jsApiList) {
	return new Promise<boolean>(async (resolve) => {
		const url = encodeURIComponent(location.href.split('#')[0]);
		const param = {
			flag: '105',
			spaceid,
			thirdpay_type: 'weixin_pubno',
			url
		};
		const cfg = await service<IWeixinConfigParam>(PAYSERVICE, param, false, false);
		wx_config({
			appId: cfg.appid,
			debug: false,
			jsApiList,
			nonceStr: cfg.nonce,
			signature: cfg.signature,
			timestamp: cfg.timestamp
		});
		ready(() => {
			resolve(true);
		});
		error((res) => {
			console.error('-调用微信公众号配置完成服务失败--错误信息---', res ? res.toString() : '');
			resolve(false);
		});
	});
}
